<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入地点名称" suffix-icon="el-icon-search" v-model="name"></el-input>
      <el-input style="width: 200px" placeholder="请输入救援方式" suffix-icon="el-icon-search" class="ml-5" v-model="rescue_way"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-select v-model="check" filterable placeholder="请选择信息是否核实">
        <el-option
            v-for="item in checkOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-select v-model="contact" class="ml-5" filterable placeholder="请选择是否取得联系">
        <el-option
            v-for="item in contactOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-select v-model="rescue" class="ml-5" filterable placeholder="请选择是否成功救援">
        <el-option
            v-for="item in rescueOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <el-upload action="http://localhost:9090/helps/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>
      </el-upload>
      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe  :header-cell-class-name="'header'" :header-cell-style="{textAlign: 'center'}" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <!--      <el-table-column fixed="left" prop="id" label="ID" width="80" sortable></el-table-column>-->
      <el-table-column
          type="index"
          width="50">
      </el-table-column>
      <el-table-column prop="name" label="地点名称" width="120" sortable :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="time" label="求助时间" width="120" sortable :show-overflow-tooltip='true'></el-table-column>
      <!--      <el-table-column prop="priority" label="紧急度"></el-table-column>-->
      <el-table-column prop="priority" label="紧急度" sortable width="145">
        <template slot-scope="scope" >
          <el-rate
              v-model="scope.row.priority"
              :colors="colors"
              disabled></el-rate>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="求助者电话" width="100" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="trappedPersons" label="被困人员" width="80" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="content" label="受灾情况描述" width="120" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="rescueWay" label="需要何种救援方式" width="140" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="isCheck" label="求助信息是否核实" width="160"  align="center" sortable>
        <template slot-scope="scope" >
          <el-tag type="success" v-if="scope.row.isCheck">已核实</el-tag>
          <el-tag type="warning" v-else>暂未核实</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isContact" label="是否和救援人员取得联系" width="200"  align="center" sortable>
        <template slot-scope="scope" >
          <el-tag type="" v-if="scope.row.isContact">是</el-tag>
          <el-tag type="warning" v-else>否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isRescue" label="是否成功救援" width="140" align="center" sortable>
        <template slot-scope="scope" >
          <el-tag effect="dark" type="success" v-if="scope.row.isRescue">已成功救援</el-tag>
          <el-tag effect="dark" type="danger" v-else>等待救援</el-tag>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作"  width="280" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="地点名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="求助时间">
          <el-date-picker v-model="form.time" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="紧急度" prop="priority">
          <el-rate v-model="form.priority" :colors="colors"></el-rate>
        </el-form-item>
        <el-form-item label="求助者电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="被困人员">
          <el-input v-model="form.trappedPersons" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="受灾情况描述">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="需要何种救援方式">
          <el-input v-model="form.rescueWay" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="求助信息是否核实" prop="isCheck">
          <el-select v-model="checkForm">
            <el-option label="已核实" value="1"></el-option>
            <el-option label="暂未核实" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否和救援人员取得联系" prop="isContact">
          <el-select v-model="contactForm">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否成功救援" prop="isRescue">
          <el-select v-model="rescueForm">
            <el-option label="已成功救援" value="1"></el-option>
            <el-option label="等待救援" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import jwtDecode from 'jwt-decode'
export default {
  name: "Helps",
  data() {
    return {
      colors:['#99A9BF', '#F7BA2A', '#EB5E26'],
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      rescue_way: "",
      check: null,
      contact: null,
      rescue: null,
      checkOptions: [{value: '1', label: '已核实'}, {value: '0', label: '暂未核实'}],
      contactOptions: [{value: '1', label: '是'}, {value: '0', label: '否'}],
      rescueOptions: [{value: '1', label: '已成功救援'}, {value: '0', label: '等待救援'}],

      form: {},
      checkForm: "",
      contactForm: "",
      rescueForm: "",
      dialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      console.log(this.user)

      this.request.get("/helps/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          rescue_way: this.rescue_way,
          check: this.check,
          contact: this.contact,
          rescue: this.rescue,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    save() {
      if (this.checkForm == 1 || this.checkForm == 0){
        this.form.isCheck = this.checkForm
      }
      if (this.contactForm == 1 || this.contactForm == 0){
        this.form.isContact = this.contactForm
      }
      if (this.rescueForm == 1 || this.rescueForm == 0){
        this.form.isRescue = this.rescueForm
      }

      console.log("form:", this.form)
      const code = jwtDecode(this.user.token)
      console.log(code.aud)
      this.form.userId = code.aud
      this.request.post("/helps", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if(this.$refs.img) {
          this.$refs.img.clearFiles();
        }
        if(this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      if (this.form.isCheck == 1){
        this.checkForm = "已核实"
      }else {
        this.checkForm = "暂未核实"
      }
      if (this.form.isContact == 1){
        this.contactForm = "是"
      }else {
        this.contactForm = "否"
      }
      if (this.form.isRescue == 1){
        this.rescueForm = "已成功救援"
      }else {
        this.rescueForm = "等待救援"
      }
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if(this.$refs.img) {
          this.$refs.img.clearFiles();
        }
        if(this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    del(id) {
      this.request.delete("/helps/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/helps/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.rescue_way = ""
      this.check = null
      this.contact = null
      this.rescue = null
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    download(url) {
      window.open(url)
    },
    exp() {
      window.open("http://localhost:9090/helps/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    }
  }
}
</script>


<style>
.header{
  /*background: #eee!important;*/
  color: rgb(102, 102, 102);
  font-size: 14px;
}
.el-tooltip__popper{
  max-width:20%;
}
.el-tooltip__popper,.el-tooltip__popper.is-dark{
  background:rgb(48, 65, 86) !important;
  color: #fff !important;
  line-height: 24px;
}
</style>
